package com.aaa.stock.service.medic.impl;

import com.aaa.entity.Medicines;
import com.aaa.stock.dao.medic.MedicinesMapper;
import com.aaa.stock.service.medic.MedicinalService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Author GaoYa
 * @Date 2023/4/4 18:32
 * @Version 1.5
 */
@Service
public class MedicinalServiceImpl implements MedicinalService {

    @Autowired
    private MedicinesMapper medicinesMapper;

    @Override
    public Boolean num(String num, String itemName) {
        BigDecimal bigDecimal = new BigDecimal(num);
        QueryWrapper<Medicines> wrapper=new QueryWrapper<>();
        wrapper.eq("medicines_name",itemName);
        Medicines medicines = medicinesMapper.selectOne(wrapper);
        BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(medicines.getMedicinesStockNum()));
        BigDecimal subtract = bigDecimal1.subtract(bigDecimal);
        if (subtract.compareTo(BigDecimal.valueOf(0))>-1) {
            BigDecimal i=bigDecimal1.subtract(bigDecimal);
            Medicines medicines1 = new Medicines();
            Integer i1 = i.intValue();
            medicines1.setMedicinesStockNum(BigDecimal.valueOf(i1));
            QueryWrapper<Medicines> wrapper1=new QueryWrapper<>();
            wrapper1.eq("medicines_name",itemName);
            int update = medicinesMapper.update(medicines1, wrapper1);
            return update>0?true:false;
        }
        return false;
    }
}
